/**
 * 
 */
package com.eagle.coders.server.db.configurator;

import java.io.Reader;

import org.apache.ibatis.builder.xml.XMLMapperBuilder;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

/**
 * @author Anees
 * 
 */
public class MyBatisConfigurator {

	private static MyBatisConfigurator configurator;

	private SqlSessionFactory factory;
	

	private MyBatisConfigurator() {
		try{
			createSqlSession();
		}catch(Exception e){
			e.printStackTrace();
		}
	}

	/**
	 * @Singleton
	 * @return
	 */
	public static MyBatisConfigurator getInstance() {
		if (null == configurator)
			configurator = new MyBatisConfigurator();
		return configurator;
	}

	/**
	 * 
	 * @throws Exception
	 */
	private void createSqlSession() throws Exception {
		String resource = "com/eagle/coders/server/db/configurator/configuration.xml";
		Reader reader = Resources.getResourceAsReader(resource);
		SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
		Configuration config = new Configuration();
		factory = builder.build(reader,"byJDBC");
	}
	
	/**
	 * 
	 */
	private void createXmlMapper(){
//		XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(inputStream, configuration, resource, sqlFragments)
	}
	
	/**
	 * 
	 * @param <T>
	 * @param clzz
	 * @return
	 */
	public <T extends Object> T getMapper(Class<T> clzz){
		if(!factory.getConfiguration().hasMapper(clzz))
			factory.getConfiguration().addMapper(clzz);
		SqlSession sqlSession = factory.openSession();
		
		T mapper = sqlSession.getMapper(clzz);
		
//		sqlSession.close();
		return mapper;
	}
	
	/**
	 * 
	 * @param namespace
	 * @param resource
	 * @param id
	 * @param paramter
	 * @return
	 */
	public Object processUnique(String namespace, String resource, String id, Object paramter){
		if(!factory.getConfiguration().isResourceLoaded(resource))
			factory.getConfiguration().addResource(namespace, resource);
		SqlSession sqlSession = factory.openSession();
		StringBuffer statementBuffer = new StringBuffer();
		statementBuffer.append(namespace);
		statementBuffer.append(".");
		statementBuffer.append(id);
		Object obj = sqlSession.selectOne(statementBuffer.toString(), paramter);
		sqlSession.close();
		return obj;
	}
	
	/**
	 * 
	 * @param namespace
	 * @param resource
	 * @param id
	 */
	public void processForCollection(String namespace, String resource, String id, Object paramter){
		
		if(!factory.getConfiguration().isResourceLoaded(resource))
			factory.getConfiguration().addResource(namespace, resource);
		SqlSession sqlSession = factory.openSession();
		StringBuffer statementBuffer = new StringBuffer();
		statementBuffer.append(namespace);
		statementBuffer.append(".");
		statementBuffer.append(id);

//		sqlSession.select
	}

}
